package cn.agiledata.bank.common.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import uk.ltd.getahead.dwr.WebContextFactory;

public class WebUtil {
	
    public static void removeApplicationObjectByKey(ServletContext context,
            Object key, String attributeName) {
        Map map = (Map) context.getAttribute(attributeName);
        if (map != null && key != null) {
            if (map.containsKey(key)) {
                map.remove(key);
            }
        }
        context.setAttribute(attributeName, map);
    }

    public static void removeSessionAttribute(HttpServletRequest request,
            String attributeName) {
        request.getSession().removeAttribute(attributeName);
    }

    /**
     * 设置到application范围内，map,key为id,value为sessionId
     * 
     * @param context
     * @param key
     * @param value
     * @param attributeName
     */
    public static void setApplicationMap(ServletContext context, Object key,
            Object value, String attributeName) {
        Map map = (Map) context.getAttribute(attributeName);
        if (map == null) {
            map = new HashMap();
        } else {
            map.put(key, value);
        }
        context.setAttribute(attributeName, map);
    }

    public static String getSessionString(HttpServletRequest request,
            String attributeName) {
        return (String) request.getSession().getAttribute(attributeName);
    }

    public static Object getSessionObject(HttpServletRequest request,
            String attributeName) {
        return request.getSession().getAttribute(attributeName);
    }

    public static void setSessionString(HttpServletRequest request,
            String attributeName, Object attributeValue) {
        request.getSession().setAttribute(attributeName, attributeValue);
    }

    public static Map getRequestParameters(HttpServletRequest request) {
        Map paramMap = new HashMap(request.getParameterMap());
        for (Iterator it = paramMap.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            Object value = entry.getValue();
            if (value instanceof Object[]) {
                Object[] values = (Object[]) value;
                if (values.length == 1)
                    entry.setValue(values[0]);
            }
        }
        return paramMap;
    }

    public static Map getSessionAttributesByClass(HttpSession session,
            Class clazz) {
        Map map = new HashMap();
        for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object value = session.getAttribute(name);
            if (clazz.isAssignableFrom(value.getClass())) {
                map.put(name, value);
            }
        }
        return map;
    }

    /**
     * 从application范围内，得到map相应key的value
     * 
     * @param context
     * @param key
     * @param attributeName
     * @return
     */
    public static Object getApplicationValueByKey(ServletContext context,
            Object key, String attributeName) {
        Map map = (Map) context.getAttribute(attributeName);
        if (map != null) {
            Object value = map.get(key);
            return value;
        } else {
            return null;
        }
    }

    public static String getRoot(HttpServletRequest request) {
        String contextPath = request.getContextPath();
        if (contextPath.length() == 0) {
        	return Constant.WEBROOT_URL;
        } else {
           return Constant.WEBROOT_URL + contextPath;
        }
    }

    public static HttpSession getSession() {
        return WebContextFactory.get().getSession();
    }
    
}
